package com.spotmanger.project.modular.orgmanger.controller;

import com.spotmanger.project.common.core.aop.annotation.LogAnnotation;
import com.spotmanger.project.common.core.exception.BusinessException;
import com.spotmanger.project.common.core.exception.code.BaseResponseCode;
import com.spotmanger.project.common.core.utils.DataResult;
import com.spotmanger.project.modular.orgmanger.entity.SysDivisionEntity;
import com.spotmanger.project.modular.orgmanger.service.SysDivisionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
/**
 * 系统部门
 *
 * @author leihao
 * @email 1321451088@qq.com
 * @date 2021-09-06 22:27:58
 */
@Controller
@RequestMapping("/")
public class SysDivisionController {
    @Autowired
    private SysDivisionService sysDivisionService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysDivision")
    public String sysDivision() {
        return "/sysDivision/list";
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("sysDivision/delete")
    @RequiresPermissions("sysDivision:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysDivisionService.removeByIds(ids);
        return DataResult.success();
    }

    @GetMapping("/sysDivisions")
    @ApiOperation(value = "查询所有记录")
    @LogAnnotation(title = "行政区划管理", action = "获取所有行政区划")
    @ResponseBody
    public DataResult getDivisionAll() {
        List<SysDivisionEntity> divisionEntityList = sysDivisionService.list();
        for (SysDivisionEntity divisionParent : divisionEntityList) {
            for (SysDivisionEntity divisionChild : divisionEntityList) {
                if (divisionChild.getPid().equals(divisionParent.getId())) {
                    divisionParent.setPidName(divisionParent.getName());
                }
            }
        }
        return DataResult.success(divisionEntityList);
    }

    @PostMapping("/sysDivision/search")
    @ApiOperation(value = "查询记录")
    @LogAnnotation(title = "行政区划管理", action = "获取所有行政区划")
    @ResponseBody
    public DataResult getSearch(@RequestBody @ApiParam(value = "区划名称") String divisionName) {
        List<SysDivisionEntity> divisionEntityList = sysDivisionService.list();
        for (SysDivisionEntity sysDivision : divisionEntityList) {
            if (sysDivision.getName().equals(divisionName)) {
                return DataResult.success(sysDivision);
            }
        }
        return DataResult.fail("查询失败");
    }

    @GetMapping("/sysDivision/tree")
    @ApiOperation(value = "树型组织列表接口")
    @LogAnnotation(title = "机构管理", action = "树型组织列表")
    @RequiresPermissions(value = {"sysDivision:add", "sysDivision:update"}, logical = Logical.OR)
    @ResponseBody
    public DataResult getTree(@RequestParam(required = false) String divisionId) {
        return DataResult.success(sysDivisionService.divisionTreeList(divisionId, false));
    }

    @PostMapping("/updateSysDivision")
    @ApiOperation(value = "更新组织信息接口")
    @LogAnnotation(title = "机构管理", action = "更新组织信息")
    @RequiresPermissions("sysDivision:update")
    @ResponseBody
    public DataResult updateDivision(@RequestBody SysDivisionEntity vo) {
        //遍历所有记录，根据传回的上级行政区划名称，设置vo的Pid
        List<SysDivisionEntity> divisionEntityList = sysDivisionService.list();
        if (vo.getPidName() == null) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        } else {
            for (SysDivisionEntity eachEntity : divisionEntityList) {
                if (eachEntity.getName().equals(vo.getPidName())) {
                    vo.setPid(eachEntity.getId());
                }
            }
        }
        if (StringUtils.isEmpty(vo.getId())) {
            return DataResult.fail("id不能为空");
        }
        sysDivisionService.updateDivision(vo);
        return DataResult.success();
    }

    @PostMapping("/addSysDivision")
    @ApiOperation(value = "新增组织接口")
    @LogAnnotation(title = "机构管理", action = "新增组织")
    @RequiresPermissions("sysDivision:add")
    @ResponseBody
    public DataResult addDept(@RequestBody @Valid SysDivisionEntity vo) {
        //遍历所有记录，根据传回的上级行政区划名称，设置vo的Pid

        List<SysDivisionEntity> divisionEntityList = sysDivisionService.list();
        if (vo.getPidName() == null) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        } else {
            for (SysDivisionEntity eachEntity : divisionEntityList) {
                if (eachEntity.getName().equals(vo.getPidName())) {
                    vo.setPid(eachEntity.getId());
                }
            }
        }

        sysDivisionService.addDivision(vo);
        return DataResult.success();
    }

    @GetMapping("/division/{id}")
    @ApiOperation(value = "查询组织详情接口")
    @LogAnnotation(title = "机构管理", action = "查询组织详情")
    public DataResult detailInfo(@PathVariable("id") String id) {
        return DataResult.success(sysDivisionService.getById(id));
    }
}
